local H = require "packages.ol_hegemony.util"

local chensu = fk.CreateSkill{
  name = "wk_heg__chensu",
  dynamic_desc = function (self, player, lang)
    if H.getActualGeneral(player, false) == "wk_heg__buzhi" then
      return "wk_heg__chensu_pro"
    elseif H.getActualGeneral(player, true) == "wk_heg__buzhi" then
      return "wk_heg__chensu_deputy"
    else
      return "wk_heg__chensu_normal"
    end
  end
}

Fk:loadTranslationTable{
  ["wk_heg__chensu"] = "沉肃",
  [":wk_heg__chensu"] = "你未使用过牌的回合内，你可以将两张类别不同的牌当【无懈可击】使用，然后摸一张牌。若你为：主将，“类别”改为“颜色”；副将，“不同”改为“相同”。",

  ["#wk_heg__chensu_pro"] = "沉肃：你可以将两张颜色不同的牌当【无懈可击】使用，然后摸一张牌",
  ["#wk_heg__chensu_deputy"] = "沉肃：你可以将两张类别相同的牌当【无懈可击】使用，然后摸一张牌" ,
  ["#wk_heg__chensu_normal"] = "沉肃：你可以将两张类别不同的牌当【无懈可击】使用，然后摸一张牌",

  [":wk_heg__chensu_pro"] = "你未使用过牌的回合内，你可以将两张<s>类别</s><font color = \"#E0DB2F\">颜色</font>不同的牌当【无懈可击】使用，然后摸一张牌。"..
    "<font color = \"#E0DB2F\">若你为：主将，“类别”改为“颜色”；</font><font color = \"gray\">副将，“不同”改为“相同”。</font>",
  [":wk_heg__chensu_deputy"] = "你未使用过牌的回合内，你可以将两张类别<s>不同</s><font color = \"#E0DB2F\">相同</font>的牌当【无懈可击】使用，然后摸一张牌。"..
    "<font color = \"gray\">若你为：主将，“类别”改为“颜色”；</font><font color = \"#E0DB2F\">副将，“不同”改为“相同”。</font>",
  [":wk_heg__chensu_normal"] = "你未使用过牌的回合内，你可以将两张类别不同的牌当【无懈可击】使用，然后摸一张牌。"..
    "<font color = \"gray\">若你为：主将，“类别”改为“颜色”；副将，“不同”改为“相同”。</font>"
}

chensu:addEffect("viewas", {
  anim_type = "special",
  prompt = function (self, player, selected_cards, selected)
    if H.getActualGeneral(player, false) == "wk_heg__buzhi" then
      return "#wk_heg__chensu_pro"
    elseif H.getActualGeneral(player, true) == "wk_heg__buzhi" then
      return "#wk_heg__chensu_deputy"
    else
      return "#wk_heg__chensu_normal"
    end
  end,
  pattern = "nullification",
  card_filter = function(self, player, to_select, selected)
    if #selected >= 2 then return end
    if #selected == 0 then
      return true
    end
    local origin = Fk:getCardById(selected[1])
    local to_select_card = Fk:getCardById(to_select)
    if H.getActualGeneral(player, false) == "wk_heg__buzhi" then
      return origin.color ~= to_select_card.color
    elseif H.getActualGeneral(player, true) == "wk_heg__buzhi" then
      return origin.type == to_select_card.type
    else
      return origin.type ~= to_select_card.type
    end
  end,
  view_as = function(self, player, cards)
    if #cards < 2 then return end
    local card = Fk:cloneCard("nullification")
    card.skillName = chensu.name
    card:addSubcards(cards)
    return card
  end,
  after_use = function(self, player, use)
    player:drawCards(1, chensu.name)
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function (self, player, response)
    if not (player:getMark("_wk_heg__chensu-turn") == 0) then return false end
    return player:getHandcardNum() > 1 and not response
  end
})

chensu:addEffect(fk.EventAcquireSkill,{
  can_refresh = function(self, event, target, player, data)
    if not (player:hasSkill(chensu.name) or player:isFakeSkill(chensu.name)) then return false end
    return target == player and data == chensu
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if room:getTag("RoundCount") then
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data
        if use.from == player and player:getMark("_wk_heg__chensu-turn") == 0 then
          room:setPlayerMark(player, "_wk_heg__chensu-turn", 1)
        end
      end, Player.HistoryTurn)
    end
  end,
})

chensu:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return (player:hasSkill(chensu.name) or player:isFakeSkill(chensu.name)) and player == target
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_wk_heg__chensu-turn", 1)
  end,
})

return chensu